package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;

import config.ColorConfig;

@SuppressWarnings("serial")
public class BottomPanel extends JPanel{

	private Map<String,JButton> jbuttons;
	private Map<String,JButton> jbuttonDeletes;
	@SuppressWarnings("unused")
	private int count;
	private int width = 1200;
	private int height = 25;
	private int bwidth = 50;
	private int bheight = 25;
	private int bdwidth = 25;
	private int bdheight = 25;
	private int ytop = 0;
	
	
	BottomPanel(){
		super();
		count = 0;
		jbuttons = new HashMap<String,JButton>();
		jbuttonDeletes = new HashMap<String,JButton>();
	}

	public void start() {
		// TODO Auto-generated method stub
		this.setLayout(null);
		setBorder(BorderFactory.createLineBorder(ColorConfig.getColor("bottomColor")));
		setPreferredSize(new Dimension(width,height));
		int temp=0;
		for(String name:jbuttons.keySet()) {
			JButton jb = jbuttons.get(name);
			jb.setBounds(temp*(bdwidth+bwidth),ytop,bwidth,bheight);
			this.add(jb);
			jb = jbuttonDeletes.get(name);
			jb.setBounds(temp*(bdwidth+bwidth)+bwidth,ytop,bdwidth,bdheight);
			this.add(jb);
			temp++;
		}
	}
	
	
	
	public void addButton(String name) {
		if(jbuttons.containsKey(name)) {
			
		}else {
			JButton jbutton = new JButton(name);
			JButton jbutton1 = new JButton("x");
			jbutton.setOpaque(true);
			jbutton1.setOpaque(true);
			jbutton.setBackground(Color.GRAY);
			jbutton1.setBackground(Color.RED);
			jbutton.setBorder(BorderFactory.createLineBorder(ColorConfig.getColor("bottomBColor")));
			jbutton1.setBorder(BorderFactory.createLineBorder(ColorConfig.getColor("bottomBDColor")));
			jbutton.addActionListener(e->{
				buttonAction(name);
			});
			jbutton1.addActionListener(e->{
				GUIManager.delPanel(name);
			});
			jbuttons.put(name,jbutton);
			jbuttonDeletes.put(name,jbutton1);
			buttonAction(name);
			count++;
			removeAll();
			start();
		}
	}
	
	public void buttonAction(String name) {
		// TODO Auto-generated method stub
		buttonMark(name);
		GUIManager.changePanel(name);
	}
	
	public void buttonMark(String name) {
		// TODO Auto-generated method stub
		for(JButton jb:jbuttons.values())
			jb.setBackground(Color.gray);
		JButton jb = jbuttons.get(name);
		if(jb != null) {
			jb.setBackground(Color.blue);
		}
	}

	public void delButton(String name) {
		if(jbuttons.containsKey(name)) {
			jbuttons.remove(name);
			jbuttonDeletes.remove(name);
		}else {
		}
		count--;
		removeAll();
		start();
	}
}
